using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CPGVertragsverwaltung.ErrorHandling;
using Infragistics.Win.UltraWinListView;
using System.Diagnostics;
using Microsoft.VisualBasic;

namespace CPGVertragsverwaltung.Viewer
{
    public partial class AttachmentEditor : Form
    {
        private String mFolderPath="";
        public AttachmentEditor()
        {
            InitializeComponent();
        }

        public String FolderPath
        {
            get { return mFolderPath; }
            set 
            {
                Boolean ShouldLoadFiles = false;
                if (mFolderPath != value && mFolderPath != "")
                    ShouldLoadFiles = true;

                mFolderPath = value; 
                //Create Folder if it doesn't Exists
                if (!Directory.Exists(mFolderPath))
                {
                    DirectoryInfo DirInfo = null;
                    try
                    {
                        DirInfo=Directory.CreateDirectory(mFolderPath);
                    }
                    catch (Exception ex)
                    {
                        MessageHandling.showError("Ordner konnte nicht erstellt werden '" + mFolderPath + "'",true);
                    }
                }
                if (ShouldLoadFiles)
                    loadFiles();
            }
        }

        private void loadFiles()
        {
            if (!Directory.Exists(mFolderPath))
                return;

            DirectoryInfo DirInfo = new DirectoryInfo(mFolderPath);

            lstFiles.Items.Clear();
            foreach (FileInfo FInfo in DirInfo.GetFiles())
            {
                String[] SubItems=new String[2];
                SubItems[0]=FInfo.CreationTime.ToString();SubItems[1]=FInfo.LastWriteTime.ToString();
                UltraListViewItem myItem = new UltraListViewItem(FInfo.Name,SubItems);
                myItem.Key = FInfo.FullName;

                myItem.Appearance.Image=Icon.ExtractAssociatedIcon(FInfo.FullName).ToBitmap();
                lstFiles.Items.Add(myItem);
                Application.DoEvents();
            }
        }

        private void AttachmentEditor_Load(object sender, EventArgs e)
        {
           
        }

        private void bttDelete_Click(object sender, EventArgs e)
        {
            foreach (UltraListViewItem myItem in lstFiles.Items)
            {
                if (myItem.CheckState != CheckState.Unchecked)
                {
                    File.Delete(myItem.Key);
                }
            }
            loadFiles();
        }

        private void AttachmentEditor_Shown(object sender, EventArgs e)
        {
            loadFiles();
        }

        private void bttAdd_Click(object sender, EventArgs e)
        {
            if (dlgOpenFile1.ShowDialog() == DialogResult.OK)
            {
                dlgOpenFile1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
                foreach (String myFilePath in dlgOpenFile1.FileNames)
                {
                    FileInfo FInfo = new FileInfo(myFilePath);

                    File.Copy(myFilePath,mFolderPath + @"\" + FInfo.Name,true);
                    Application.DoEvents();
                }
            }
            loadFiles();
        }
    }
}